const router = require('koa-router')();
const {checkSession} = require('../utils/utils');
// 新闻模块
const newsRouter = require('./news');
// 商品模块
const productRouter = require('./product');
// 评论模块
const commentRouter = require('./comment');
// 公告模块
const noticeRouter = require('./notice');
// 商品分类模块
const catagoryRouter = require('./catagory');
// 购物车模块
const shopCarRouter = require('./shopCar');
// 订单模块
const orderRouter = require('./order');
// 白名单
const whiteList = ['/auth/notice', '/auth/news', '/auth/product']


router.prefix('/auth');
router.all('*', async (ctx, next) => {
    if (whiteList.includes(ctx.request.url)){
        return await next();
    }
    const session = checkSession(ctx);
    if (session) {
        await next();
    } else {
        ctx.unAuth(
            '未登录或登录过期',
        );
    }
});

router.use('', newsRouter.routes(), newsRouter.allowedMethods());
router.use('', productRouter.routes(), productRouter.allowedMethods());
router.use('', commentRouter.routes(), commentRouter.allowedMethods());
router.use('', noticeRouter.routes(), noticeRouter.allowedMethods());
router.use('', catagoryRouter.routes(), catagoryRouter.allowedMethods());
router.use('', shopCarRouter.routes(), shopCarRouter.allowedMethods());
router.use('', orderRouter.routes(), orderRouter.allowedMethods());

module.exports = router;